    /**
     * @name Arrematantes
     * @description Arrematantes controllers 
     * 
     */
(function(a) {
    'use strict';
    /**
     * 
     * @object @exp;a@pro;adminApp
     * 
     */
    var app = a.adminApp;
   
    app.controller(
            'arrematantesController.Index',
            function($scope, RestFactory, cocLoading, $location, $timeout, notificationFactory, $localStorage) {

                $scope.list = [];
                $scope.orderByColumn = 'ID';
                $scope.reverse = true;


                var ctr = {
                    api: '/api/arrematantes',
                    load: function() {
                        cocLoading.trigger();
                        var self = this;
                        RestFactory.get(self.api).success(function(data) {
                            $scope.list = data;
                            $scope.currentPage = self.currentPage(); //current page
                            $scope.entryLimit = 20; //max no of items to display in a page
                            $scope.filteredItems = $scope.list.length; //Initially for no filter 
                            $scope.totalItems = $scope.list.length;
                            $timeout(function() {
                                cocLoading.trigger();
                            }, 10);
                        }).error(function(data) {
                            $timeout(function() {
                                cocLoading.trigger();
                            }, 10);
                        });
                    },
                    delete: function(id) {
                        RestFactory.delete(this.api + '/' + id).success(function(data) {
                            notificationFactory.success(data.message);
                            ctr.load();
                        }).error(function(data) {
                            notificationFactory.error(data.message);
                        });
                    },
                    currentPage: function() {

                        var hasPage = $localStorage.get(this.$controller);
                        if (!hasPage) {
                            $localStorage.set(this.$controller, 1);
                            hasPage = 1;
                        }
                        return hasPage;
                    },
                    $controller: 'page_' + $location.$$absUrl,
                    $currentPage: function(pageNo) {
                        $localStorage.set(this.$controller, pageNo);
                    }
                };

                ctr.load();
                $scope.setPage = function(pageNo) {
                    $scope.currentPage = pageNo;
                    ctr.$currentPage(pageNo);
                };
                $scope.filter = function() {
                    $timeout(function() {
                        $scope.filteredItems = $scope.filtered.length;
                    }, 10);
                };
                $scope.resetSearch = function() {
                    $scope.filteredItems = $scope.list.length;
                    $scope.search = '';
                };

                $scope.orderByFnc = function(item, forceString) {
                    var forceStr = forceString || false;
                    if (isNaN(item[$scope.orderByColumn]) || forceString) {
                        return item[$scope.orderByColumn];
                    }
                    return parseInt(item[$scope.orderByColumn], 10);
                };
                $scope.sort_by = function(predicate) {
                    //$scope.predicate = predicate;
                    $scope.orderByColumn = predicate;
                    $scope.reverse = !$scope.reverse;
                };


                $scope.delete = function(id) {
                    notificationFactory.confirm({
                        message: 'Deseja apagar esse registro ?',
                        callbackConfirm: function() {
                            ctr.delete(id);
                        }
                    });
                };


            });
    app.controller('arrematantesController.Frm', function($scope, $location) {
        $scope.tipoPessoa = 'J';

        $scope.onNext = function() {
            $location.path('/buyers/add/' + $scope.tipoPessoa);
        };

    });
    app.controller(
            'arrematantesController.fisica',
            function($scope, $location, $routeParams, $timeout,
                    cocGridDataFactory, notificationFactory, RestFactory, $filter, cocLoading) {
                var cepPattern = /[0-9]{8}/gi;
                $scope.arrematante = {};
                $scope.arrematante.TIPO = 'F';
                var $_id = $routeParams.id || 0;
                $scope.hideButtonCancel = false;
                cocLoading.trigger();

                $scope.add = true;
                $scope.edit = false;

                if ($_id > 0) {
                    $scope.add = !$scope.add;
                    $scope.edit = !$scope.edit;

                }
                $scope.estadoCivilOptions = [{
                        id: 'CASADO',
                        label: 'CASADO(A)'
                    }, {
                        id: 'DIVORCIADO',
                        label: 'DIVORCIADO(A)'
                    }, {
                        id: 'SOLTEIRO',
                        label: 'SOLTEIRO(A)'
                    }, {
                        id: 'VIUVO',
                        label: 'VIÚVO(A)'
                    }];

                shortcut
                        .add(
                                "Ctrl+S",
                                function() {
                                    if ($scope.ArrematanteForm.$valid) {
                                        $scope.save();
                                    } else {
                                        notificationFactory
                                                .error('Verifique os campos obrigatórios, esse formulário não pode ser submetido');
                                    }

                                });
                // auto complete cep
                $scope.setLogradouro = function() {
                    if (cepPattern.test($scope.arrematante.CEP)
                            && $scope.LastCEP !== $scope.arrematante.CEP) {
                        // toogleLoadingForm();
                        cocGridDataFactory('logradouro')
                                .get(
                                        {
                                            'CEP': $scope.arrematante.CEP
                                        },
                                function(obj) {
                                    $scope.arrematante.LOGRADOURO = obj.LOGRADOURO;
                                    $scope.arrematante.BAIRRO = obj.BAIRRO;
                                    $scope.arrematante.CIDADE = obj.CIDADE;
                                    $scope.arrematante.UF = obj.UF;
                                    $scope.arrematante.TIPO_LOGRADOURO = obj.TIPO_LOGRADOURO;

                                    // toogleLoadingForm();
                                });
                    }
                    $scope.LastCEP = $scope.arrematante.CEP;
                };

                $scope.save = function($redirect) {
                    var $redirect = $redirect === 'true' ? true : false;
                    cocLoading.trigger();
                    // $scope.hideform = true;
                    if ($_id) {
                        cocGridDataFactory('arrematantes').update({
                            'id': $_id
                        }, $scope.arrematante, function() {
                            notificationFactory.success();
                            cocLoading.trigger();
                        }, function(e) {

                            notificationFactory.error(e.data.message);
                            cocLoading.trigger();
                        });

                    } else {

                        cocGridDataFactory('arrematantes').save(
                                $scope.arrematante,
                                function() {
                                    notificationFactory.success();
                                    $scope.arrematante = {};
                                    cocLoading.trigger();
                                },
                                function(e) {
                                    cocLoading.trigger();
                                    notificationFactory
                                            .error(e.data.message);
                                });

                    }

                    if ($redirect) {
                        $location.path('/buyers');
                    }

                };

                if ($_id > 0) {
                    $scope.hideButtonCancel = true;
                    // $timeout(function() {
                    RestFactory.get('/api/arrematantes/' + $_id).success(function(data) {
                        $scope.arrematante = data;
                        $scope.arrematante.PWD = '';
                        $scope.arrematante.NASCIMENTO = $filter('phpdate')('d/m/Y', $filter('strtotime')(data.NASCIMENTO));
                        cocLoading.trigger();
                    });

                    // }, 1000);

                } else {
                    $timeout(function() {
                        cocLoading.trigger();
                    });
                }

            });

    app.controller(
            'arrematantesController.juridica',
            function($scope, $location, $routeParams, $timeout,
                    cocGridDataFactory, notificationFactory, RestFactory, cocLoading) {
                var cepPattern = /[0-9]{8}/gi;
                $scope.arrematante = {};
                $scope.arrematante.TIPO = 'J';
                var $_id = $routeParams.id || 0;
                $scope.hideButtonCancel = false;
                cocLoading.trigger();

                $scope.add = true;
                $scope.edit = false;
                if ($_id > 0) {
                    $scope.add = !$scope.add;
                    $scope.edit = !$scope.edit;

                }
                shortcut.add(
                        "Ctrl+S",
                        function() {
                            if ($scope.ArrematanteForm.$valid) {
                                $scope.save();
                            } else {
                                notificationFactory
                                        .error('Verifique os campos obrigatórios, esse formulário não pode ser submetido');
                            }

                        });

                $scope.setLogradouro = function() {
                    if (cepPattern.test($scope.arrematante.CEP)
                            && $scope.LastCEP !== $scope.arrematante.CEP) {
                        cocLoading.trigger();
                        cocGridDataFactory('logradouro')
                                .get(
                                        {
                                            'CEP': $scope.arrematante.CEP
                                        },
                                function(obj) {
                                    $scope.arrematante.LOGRADOURO = obj.LOGRADOURO;
                                    $scope.arrematante.BAIRRO = obj.BAIRRO;
                                    $scope.arrematante.CIDADE = obj.CIDADE;
                                    $scope.arrematante.UF = obj.UF;
                                    $scope.arrematante.TIPO_LOGRADOURO = obj.TIPO_LOGRADOURO;

                                    cocLoading.trigger();
                                });
                    }
                    $scope.LastCEP = $scope.arrematante.CEP;
                };

                $scope.save = function($redirect) {
                    var $redirect = $redirect === 'true' ? true : false;
                    cocLoading.trigger();
                    if ($_id > 0) {
                        cocGridDataFactory('arrematantes').update({
                            'id': $_id
                        }, $scope.arrematante, function() {
                            notificationFactory.success();
                            cocLoading.trigger();
                        }, function(e) {

                            notificationFactory.error(e.data.message);
                            cocLoading.trigger();
                        });

                    } else {
                        cocGridDataFactory('arrematantes').save(
                                $scope.arrematante,
                                function() {
                                    notificationFactory.success();
                                    $scope.arrematante = {};
                                    cocLoading.trigger();
                                },
                                function(e) {

                                    notificationFactory
                                            .error(e.data.message);
                                    cocLoading.trigger();
                                });
                    }
                    if ($redirect) {
                        $location.path('/buyers');
                    }
                };
                if ($_id > 0) {
                    $scope.hideButtonCancel = true;
                    RestFactory.get('/api/arrematantes/' + $_id).success(function(data) {
                        $scope.arrematante = data;
                        $scope.arrematante.PWD = '';

                        cocLoading.trigger();
                    });

                } else {
                    $timeout(function() {
                        cocLoading.trigger();
                    });
                }

            });
})(cocApp);